<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="新增项目" name="first">
        <div style="width: 100%; display: flex; justify-content: center">
          <div style="
              width: 500px;
              width: 500px;
              display: flex; 
              flex-direction: column;
            ">
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
              <el-col :span="24">
                <el-form-item label="项目名称" prop="proName">
                  <el-input v-model="form.proName" placeholder="请输入项目名称" />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="项目类型" prop="proType">
                  <el-select v-model="form.proType" placeholder="请选择项目类型">
                    <el-option v-for="item in postList" :key="item.postId" :label="item.postName" :value="item.postId"
                      :disabled="item.status == 1"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="项目文件">
                  <file-upload v-model="form.proFile" />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="项目描述" prop="proDescr">
                  <el-input v-model="form.proDescr" type="textarea" placeholder="请输入项目主要描述" />
                </el-form-item>
              </el-col>
            </el-form>
            <div style="display: flex; justify-content: center">
              <el-button type="primary" @click="submitForm" style="margin-right: 15%">确 定</el-button>
              <el-button @click="cancel">清空</el-button>
            </div>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="全部项目" name="second"></el-tab-pane>
      <el-tab-pane label="已审核项目" name="third"></el-tab-pane>
    </el-tabs>
    <div v-show="checkMenu">
      <el-row :gutter="10" class="mb8">
        <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:project:add']"
        >新增</el-button>
      </el-col> -->
        <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
            v-hasPermi="['system:project:edit']">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
            v-hasPermi="['system:project:remove']">删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
            v-hasPermi="['system:project:export']">导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="projectList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="项目编号" align="center" prop="id" />
        <el-table-column label="项目名称" align="center" prop="proName" />
        <el-table-column label="项目文件" align="center" width="180px">
          <template slot-scope="scope">
            <a @click="downloadFile(scope.row)" class="fileStyle">{{
              downName(scope.row.proFile)
            }}</a>
            <el-progress :percentage="progress1" :format="format" v-if="scope.row.id == rowid ? true : false">
            </el-progress>
          </template>
        </el-table-column>
        <el-table-column label="项目主要描述" align="center" prop="proDescr" />
        <el-table-column label="项目上传人" align="center" prop="proAuthor" />
        <el-table-column label="项目上传时间" align="center" prop="proCreatetime" :formatter="dateFormat" />
        <el-table-column label="项目分组状态" align="center" prop="proGroupStatus">
          <div slot-scope="scope">
            {{
              scope.row.proGroupStatus == 0
                ? (proGroupStatus = "未分组")
                : (proGroupStatus = "已分组")
            }}
          </div>
        </el-table-column>
        <el-table-column label="所属小组" align="center" prop="groupId">
          <div slot-scope="scope">
            {{
              scope.row.groupId == null ? (groupId = "未知") : scope.row.groupId
            }}
          </div>
        </el-table-column>
        <el-table-column label="审核状态" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == 0">待审核</el-tag>
            <el-tag type="success" v-else-if="scope.row.status == 1">已通过</el-tag>
            <el-tag type="danger" v-if="scope.row.status == 2">被驳回</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200px">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-s-promotion" @click="handleProgress(scope.row)"
              v-hasPermi="['system:project:edit']">查看进度</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['system:project:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['system:project:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>
    <!-- 添加或修改项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="24">
          <el-form-item label="项目名称" prop="proName">
            <el-input v-model="form.proName" placeholder="请输入项目名称" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="项目类型" prop="proType">
            <el-select v-model="form.proType" placeholder="请选择项目类型">
              <el-option v-for="item in postList" :key="item.postId" :label="item.postName" :value="item.postId"
                :disabled="item.status == 1"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="项目文件" prop="proFile">
            <file-upload v-model="form.proFile" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="项目描述" prop="proDescr">
            <el-input v-model="form.proDescr" type="textarea" placeholder="请输入项目主要描述" />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 进度 -->
    <el-dialog title="审核进度" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <div style="margin-top: -20px">
          <el-alert title="审批状态：待审核" type="info" :closable="false" v-if="form.status == 0" center></el-alert>
          <el-alert title="审批状态：已通过" type="success" :closable="false" v-if="form.status == 1" center></el-alert>
          <el-alert title="审批状态：被驳回" type="error" :closable="false" v-if="form.status == 2" center></el-alert>
        </div>
        <el-divider></el-divider>
        <div style="height: 300px">
          <el-steps direction="vertical" :active="form.status + 2" finish-status="success">
            <el-step title="已提交" :description="'发起人：' + form.proAuthor"></el-step>
            <el-step title="待审批"></el-step>
            <el-step title="已通过审核" v-if="form.status + 2 == 3"></el-step>
            <el-step title="被驳回" v-if="form.status + 2 == 4"> </el-step>
          </el-steps>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProject,
  getProject,
  delProject,
  addProject,
  updateProject,
} from "@/api/system/project/project";
import download from "@/plugins/download";
import moment from "moment";
import { listPost, getPost, delPost, addPost, updatePost } from "@/api/system/post";
export default {
  name: "Project",
  data () {
    return {
      checkMenu: true,
      activeName: "second",
      postList: [],
      // 遮罩层
      baseUrl: process.env.VUE_APP_BASE_API,
      loading: true,
      rowid: "",
      statusText: "",
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目表格数据
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      progress1: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        proName: null,
        proFile: null,
        proType: null,
        proDescr: null,
        proAuthor: null,
        proCreatetime: null,
        proGroupStatus: null,
        groupId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        proName: [
          { required: true, message: "请输入项目名称", trigger: "blur" },
        ],
        proDescr: [
          { required: true, message: "请输入项目描述", trigger: "blur" },
        ],
        proType: [
          { required: true, message: "请选择项目类型" },
        ],
      },
    };
  },
  created () {
    this.getList();
  },
  computed: {
    progress () {
      return this.$store.state.myProject.progress;
    },
  },
  watch: {
    progress (newval, oldval) {
      this.progress1 = newval;
    },
  },
  methods: {
    //时间格式化
    dateFormat: function (row, column) {
      var date = row[column.property];
      if (date == undefined) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD");
    },

    handleClick (tab, event) {
      console.log(tab.index);
      if (tab.index == 0) {
        this.checkMenu = false;
        this.reset();
      } else {
        this.checkMenu = true;
        let index = 0;
        tab.index == 2 ? (index = 1) : (index = "");
        this.$store.commit("myProject/changeStatus", index);
        this.getList();
      }
    },
    format (percentage) {
      return percentage === 100 ? "完成" : `${percentage}%`;
    },
    downloadFile (data) {
      if (this.progress1 != 0) {
        this.$message({
          message: "已经有一个项目正在下载",
          type: "warning",
        });
      } else {
        this.rowid = data.id;
        download.resource(data.proFile);
        setTimeout(() => {
          this.rowid = "";
          this.progress1 = 0;
        }, 3000);
      }
    },
    downName (file) {
      let data = file.split("/");
      return data[data.length - 1];
    },
    /** 查询项目列表 */
    getList () {
      this.loading = true;
      this.queryParams.status = this.$store.state.myProject.status;
      listProject(this.queryParams).then((response) => {
        this.projectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listPost().then(response => {
        this.postList = response.rows;
        this.postList.forEach(e => {
          e.postName = e.postName.split("专家")[0];
        });
        this.postList.forEach((e, index) => {
          if (e.postName == "院长") {
            this.postList.splice(index, 1)
          }
        });
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        proName: null,
        proFile: null,
        proType: null,
        proDescr: null,
        proAuthor: null,
        proCreatetime: null,
        proGroupStatus: 0,
        groupId: null,
        userId: null,
        status: 0,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加项目";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const id = row.id || this.ids;
      getProject(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目";
      });
    },
    handleProgress (row) {
      this.reset();
      const id = row.id || this.ids;
      this.open1 = true;
      getProject(id).then((response) => {
        this.form = response.data;
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.proFile != "" && this.form.proFile != undefined) {
            if (this.form.id != null) {
              updateProject(this.form).then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addProject(this.form).then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.reset();
                this.getList();
              });
            }
          } else {
            this.$message({
              message: "你还没有上传项目",
              type: "warning",
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除项目编号为"' + ids + '"的数据项？')
        .then(function () {
          return delProject(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download(
        "system/project/export",
        {
          ...this.queryParams,
        },
        `project_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
.fileStyle {
  padding: 12px;
  color: #442ed0;
}
.fileStyle:hover {
  text-decoration: underline !important;
  cursor: pointer;
  color: #2300ff;
}
</style>
